package com.sbr.esbmonitor.wicket.pages.servers;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import com.sbr.esbmonitor.repositories.xml.impl.XMLServerRepository;
import com.sbr.esbmonitor.servicemix.Server;
import com.sbr.esbmonitor.wicket.pages.BasePage;
import com.sbr.esbmonitor.wicket.pages.queues.ListQueuesPage;

public class ListServersPage extends BasePage {
	
	public ListServersPage() {
		super();
		IModel<List<Server>> listModel = new LoadableDetachableModel<List<Server>>() {
			private static final long serialVersionUID = 1L;

			@Override
			protected List<Server> load() {
				return new XMLServerRepository().selectAll();
			}
		};
		ListView<Server> list = new ListView<Server>("server", listModel) {
 
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final ListItem<Server> item) {
				Server t = item.getModelObject();
				item.add(new Label("label", t.getLabel()));
				item.add(new Label("hostname", t.getHostName()));
//				item.add(new Image("type", t.getType() == TaskType.BUG ? 
//								BUG_IMAGE
//								: t.getType() == TaskType.ENHANCEMENT ?
//									ENHANCEMENT_IMAGE
//									: NEW_FEATURE_IMAGE));
//				if (t.getStatus() == TaskStatus.CLOSED) {
//					item.add(new SimpleAttributeModifier("class", "task closed"));
//				}
				item.add(new BookmarkablePageLink<EditServerPage>("editLink", EditServerPage.class,
						new PageParameters(Collections.singletonMap("server", t.getId()))));
				item.add(new BookmarkablePageLink<EditServerPage>("queueListLink", ListQueuesPage.class,
						new PageParameters(Collections.singletonMap("server", t.getId()))));
 
			}
		};
		add(list);
		add(new BookmarkablePageLink<EditServerPage>("addLink", EditServerPage.class));
		
	}

}
